﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.ServiceModel;
using System.Windows.Forms;
using GMPP.MapEdit;
using GMPP.utils;
using SHVS.Components;
using SHVS.Components.BuildingEdit;
using SHVS.DisplayData;
using SHVS.GeneralTools;
using SHVS.OBJMODEL;
using VLIB2D.Geometry2D;

namespace GMPP
{
    public partial class SceneForm : Form
    {
        public Scene view;

        public string path = System.AppDomain.CurrentDomain.BaseDirectory+"\\Media";
        
        public SceneForm()
        {
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(640, 480);
            this.Name = "MainForm";
            this.Text = "NeHe Lesson 20 - C#";

 


            this.view = new Scene(path) { Parent = this, Dock = DockStyle.Fill };
            Init();
            this.Show();

            //var dp = new DrawPoly();
            //dp.EventPoly += Test;
            //dp.Show();
        }


        public void Init()
        {
            #region 测试三角形

            var p1 =
                new Polygon2D(new[] { new Point2D(0, 0), new Point2D(0, 100), new Point2D(50, 100), new Point2D(100, 0) });

            var poly =
                new Polygon2D(new[] { new Point2D(0, 0), new Point2D(0, 100), new Point2D(50, 100), new Point2D(50, 0) });

            var p2 =
                new Polygon2D(new[] { new Point2D(0, 100), new Point2D(50, 200), new Point2D(100, 200), new Point2D(50, 100) });
            var p3 =
                new Polygon2D(new[] { new Point2D(50, 200), new Point2D(50, 300), new Point2D(100, 300), new Point2D(100, 200) });
            var p4 =
                new Polygon2D(new[] { new Point2D(50, 300), new Point2D(50, 350), new Point2D(100, 350), new Point2D(100, 300) });
            //Polygon2D p1 = new Polygon2D(new Point2D[] { new Point2D(0, 0), new Point2D(0, 100), new Point2D(50, 100), new Point2D(50, 0) });
            //Polygon2D p1 = new Polygon2D(new Point2D[] { new Point2D(0, 0), new Point2D(0, 100), new Point2D(50, 100), new Point2D(50, 0) });
            //Polygon2D p1 = new Polygon2D(new Point2D[] { new Point2D(0, 0), new Point2D(0, 100), new Point2D(50, 100), new Point2D(50, 0) });
            //Polygon2D p1 = new Polygon2D(new Point2D[] { new Point2D(0, 0), new Point2D(0, 100), new Point2D(50, 100), new Point2D(50, 0) });
            //List<Polygon2D> steps = new List<Polygon2D>();
            //steps.Add(p1);
            //steps.Add(p3);
            ////var pts = new Point2DList(poly.Points);
            ////var roof = new Pentroof(pts, 45, 0);
            ////Mesh mesh = SHVStools.TriangleToMeshes(roof.T3D, roof.T2D);
            ////view.AddMesh(mesh);
            //List<Polygon2D> slabs = new List<Polygon2D>();
            //slabs.Add(p2);
            //slabs.Add(p4);
            //Stairs sts = new Stairs(new Point2D(0, 0), new Point2D(100, 0), slabs, steps, 100f, 4f);
            //Mesh Wallmesh = SHVStools.TriangleToMeshes(sts.PtTriangle, sts.UvTriangle);

            //view.AddMesh(Wallmesh);

            ////SHVSGenerator.GeneratProxy GP = new GeneratProxy("9080177");

            //view.AddMesh(GP.墙体);
            //view.AddMesh(GP.地面);
            //view.AddMesh(GP.柱子);
            //List<Triangle> tts=new List<Triangle>();
            //for (int i = 0; i < sts.PtTriangle.Length;i++ )
            //{
            //    Triangle tt = new Triangle();
            //    tt.MaterialName = @"Data\brick.jpg";
            //    tt.vertex[0].coords = new float[] { (float)sts.PtTriangle[i]._x1, (float)sts.PtTriangle[i]._y1, (float)sts.PtTriangle[i]._z1, (float)sts.UvTriangle[i]._x1, (float)sts.UvTriangle[i]._y1, }; mn  m
            //    tt.vertex[1].coords = new float[] { (float)sts.PtTriangle[i]._x2, (float)sts.PtTriangle[i]._y2, (float)sts.PtTriangle[i]._z2, (float)sts.UvTriangle[i]._x2, (float)sts.UvTriangle[i]._y2, };
            //    tt.vertex[2].coords = new float[] { (float)sts.PtTriangle[i]._x3, (float)sts.PtTriangle[i]._y3, (float)sts.PtTriangle[i]._z3, (float)sts.UvTriangle[i]._x3, (float)sts.UvTriangle[i]._y3, };
            //    tts.Add(tt);

            //}

            //this.view.sector1 = tts.ToArray();
            //SHVS.OBJMODEL.ObjModel.ObjExporter("C:\\MyOBj\\" + "多段楼梯" + ".obj", Wallmesh);

            #endregion

            #region HeightMap
            var image = new Bitmap(path + "\\" + "terrain.png");

            var rect = new Rectangle(0, 0, image.Width, image.Height);
            var stopwatch = new System.Diagnostics.Stopwatch();


            var bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            stopwatch.Start();
            var hm = new HeightMap(bitmapdata, 20000, 20000, 2000, "terrain_texture.jpg");
            stopwatch.Stop();

            Debug.WriteLine("用时ms:"+stopwatch.ElapsedMilliseconds.ToString());
            var sd = new SkyDome(15, 15, 10000, "sky.jpg");
            //  var mesh = hm.Triangle5Ds.ToMesh();
            // view.AddMesh(mesh);
            view.sd = sd;
            view.Texfile.Add(sd.DomeTex);
            //view.AddTriangles5D(hm.Triangle5Ds);
            view.hm = hm;
            view.SetupPointers();

            // view.AddTriangles5D(hm.Triangle5Ds);
            // view.AddTriangles5D(sd.Triangle5Ds);
            //var trian = new Triangles5D();
            //trian.Add(hm.Triangle5Ds);
            //trian.Add(sd.Triangle5Ds);
             ObjModel.ObjExporter(@"c:\2.obj", hm.Triangle5Ds);
            //ObjModel.ObjExporter(@"c:\3.obj", sd.Triangle5Ds);
            //ObjModel.ObjExporter(@"c:\all.obj", trian);
            // ObjModel.ObjExporter(@"c:\1.obj",mesh);
            //var sp = new ShowPic(image);

            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //var client = new ServiceSoapClient("ServiceSoap");
            //bool bl = client.Endpoint.IsSystemEndpoint;
            //(client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
            //(client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;

            ////var netTcpBinding = client.Endpoint.Binding as NetTcpBinding;
            ////if (netTcpBinding != null)
            ////    netTcpBinding.MaxReceivedMessageSize = int.MaxValue;
            ////var tcpBinding = client.Endpoint.Binding as NetTcpBinding;
            ////if (tcpBinding != null)
            ////    tcpBinding.MaxBufferPoolSize = int.MaxValue;
            ////var binding = client.Endpoint.Binding as NetTcpBinding;
            ////if (binding != null)
            ////    binding.MaxBufferSize = int.MaxValue;

            ////(client.Endpoint.Binding as NetTcpBinding).MaxReceivedMessageSize = int.MaxValue;
            ////(client.Endpoint.Binding as NetTcpBinding).MaxBufferPoolSize = int.MaxValue;
            ////(client.Endpoint.Binding as NetTcpBinding).MaxBufferSize = int.MaxValue;   

            //byte[] bytes = client.Bulid_GetEditData("1345", "虹口", "高层建筑物", 1, 1, "00ffee", 4, 0, 5);
            //var be = new BuildingEdit(bytes);

            ////var bd = be.TexList[0].Bitdata;
            ////Bitmap bm = new Bitmap(bd.Width,bd.Height);
            ////bm.UnlockBits(bd);
            //for (int i = 0; i < be.TexList.Count; i++)
            //{
            //    Bitmap bm = be.TexList[i].Bitmap;
            //    var sp = new ShowPic(be.TexList[i].Bitmap);
            //}
        }

        public void Test(List<Point2D> pts)
        {
            var pt = new Point2DList(pts);
            var roof = new Pentroof(pt, 45, 0);
            Mesh mesh = SHVStools.TriangleToMeshes(roof.T3D, roof.T2D);
            view.AddMesh(mesh);
        }
    }
}